﻿using CommunityToolkit.Mvvm.Messaging;
using houseAgency.View.UserInfor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace houseAgency.Base
{
    /// <summary>
    /// 窗口管理器
    /// </summary>
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();
        public static void Register(string key)
        {
            string filePath = System.IO.Path.Combine(Environment.CurrentDirectory.Split(@"bin")[0], key);
            List<string> list = GetFileList(filePath,"*.xaml");
            foreach (string file in list)
            {
                string fileName = file.Split(".")[0];
                Type t = typen(fileName);
                if (key != null && !_RegisterWindow.Contains(fileName))
                {
                    _RegisterWindow.Add(fileName, t);
                }
            }
        }

        public static void Register(string key, Type t)
        {
            if (key != null && !_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, t);
            }
        }

        public static void Remove(string key)
        {
            if (key != null && _RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }

        public static bool? Show(string key, object VM)
        {
            bool? result = null;
            if (key != null && _RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                //win.DataContext = VM;
                if (win != null)
                {
                    result = win.ShowDialog();
                }
            }
            return result;
        }

        private static Type typen(string typeName)
        {
            Type type = null;
            Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
            int assemblyArrayLength = assemblyArray.Length;
            for (int i = 0; i < assemblyArrayLength; ++i)
            {
                type = assemblyArray[i].GetType(typeName);
                if (type != null)
                {
                    return type;
                }
            }

            for (int i = 0; (i < assemblyArrayLength); ++i)
            {
                Type[] typeArray = assemblyArray[i].GetTypes();
                int typeArrayLength = typeArray.Length;
                for (int j = 0; j < typeArrayLength; ++j)
                {
                    if (typeArray[j].Name.Equals(typeName))
                    {
                        return typeArray[j];
                    }
                }
            }
            return type;
        }

        private static List<string> GetFileList(string path, string pattern = "*")
        {
            List<string> fileList = new List<string>();

            if (Directory.Exists(path) == true)
            {
                List<FileInfo> folder = new DirectoryInfo(path).GetFiles(pattern).ToList();
                List<string> list = folder.Select(x => x.Name).ToList();
                foreach (string file in list)
                {
                    fileList.Add(file);
                }

                foreach (string directory in Directory.GetDirectories(path))
                {
                    fileList.AddRange(GetFileList(directory,pattern));
                }
            }

            return fileList;
        }
    }
}
